package com.wxy.ej.flow.modules.dynamicForm.controller;

import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.easyjava.common.utils.others.DateTool;
import com.rt.easyjava.common.utils.others.StringUtils;
import com.wxy.ej.flow.modules.dynamicForm.service.modelService.Form1Service;
import io.jsonwebtoken.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Api(tags = "JwtController -  控制类")
@RequestMapping("/coding/form/jwtPortal")
@Controller
public class JwtPortalController {
    @Autowired
    protected MyMapper mapper;
    @Autowired
    protected Form1Service form1Service;

    @Value("${jwt.shareLink}")
    private String signature;

    @ApiOperation("selectById")
    @GetMapping(value = "/selectById")
    @ResponseBody
    public Result selectById(Long id){
        return form1Service.selectById(id);
    }

    // 放开权限给分享链接使用
    @GetMapping(value = "/parseShareLinkJwt")
    @ResponseBody
    public Result parseShareLinkJwt (String jwt) {
        Claims claims = null;
        try {
            claims = getPayLoad(jwt);
        }catch (ExpiredJwtException e) {
            return Result.builder().fault("超链接过期");
        }

        JSONObject data = new JSONObject();
        data.put("dynamicFormId", claims.get("dynamicFormId"));
        data.put("expandData", claims.get("expandData"));

        return Result.builder().setObj(data).success();
    }

    //从token中取出载荷
    public Claims getPayLoad(String jwtToken){
        Claims claims = Jwts.parser()
                .setSigningKey(signature)
                .parseClaimsJws(jwtToken)
                .getBody();
        return claims;
    }

}
